package com.ai.zhly.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ai.dmc.index.base.impl.base.BaseServiceImpl;
import com.ai.dmc.index.base.sequence.SequenceGenerator;
import com.ai.dmc.index.common.bean.ScreenBean;
import com.ai.dmc.index.common.exception.BusinessException;
import com.ai.dmc.index.common.util.BasicKMeans;
import com.ai.dmc.index.common.util.DateUtil;
import com.ai.dmc.utils.json.JsonUtil;
import com.ai.frame.bean.InputObject;
import com.ai.frame.bean.OutputObject;
import com.ai.frame.logger.Logger;
import com.ai.frame.logger.LoggerFactory;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * 
* @Title: MapDataImpl.java
* @Description: 家装宽带展示层service
* @author xueyf3  
* @date 2016年5月23日 下午4:22:54
* Copyright：Copyright (c) 2016
* Company：北京亚信智慧数据科技有限公司
* @version V1.0
 */
@SuppressWarnings({ "unused", "rawtypes", "unchecked" })
public class ScreenServiceImpl extends BaseServiceImpl implements IScreenService{
	// 定义对应Mapper文件的命名空间
    private static final String NAMESPACE = "Screen.";
    
    public static Logger log = LoggerFactory.getServiceLog(ScreenServiceImpl.class);

    private SequenceGenerator sequenceGenerator;
    
    private ICommonService commonService;

    public void setSequenceGenerator(SequenceGenerator sequenceGenerator) {
        this.sequenceGenerator = sequenceGenerator;
    }
    

	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}


//	@Override
//	public void queryClassChartsList(InputObject inputObject, OutputObject outputObject) throws BusinessException {
//		// TODO Auto-generated method stub
//		Map<String, Object> resultMap = new HashMap<String, Object>();
//		inputObject.getParams().get("xxx");
//		
//		
//		resultMap.put("xxx", "1111");
//		outputObject.setObject(JsonUtil.Object2Json(resultMap));
//	}
    //全国各省市来桂人数分布
	@Override
	public void vstDistributionLastMonth(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		
		List<ScreenBean> list = this.getBaseDao().queryForList(
            NAMESPACE + "vstDistributionLastMonth", inputObject.getParams(),ScreenBean.class);
		double[] personCnt = new double[list.size()];
		int n = 0;
		for (ScreenBean bean : list) {
			personCnt[n++] = Double.parseDouble(bean.getVst_cnts());
		}

		List<Double> edgeInfo = BasicKMeans.getGroupEdgeValue(personCnt, 5);

		Map result = new HashMap();
		result.put("sourceList", list);
		result.put("edgeInfo", edgeInfo);
		JSONObject data = JSONObject.fromObject(result);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		resultMap.put("regions", null == data ? "" : JsonUtil.Object2Json(data));
        outputObject.setObject(JsonUtil.Object2Json(resultMap));
	}


	@Override
	public void cntAndmonthlydistrb(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		String bcycId=inputObject.getParams().get("bcycId");
		if (bcycId == null || "".equals(bcycId)) {
			DateUtil du = DateUtil.getInstance();
			bcycId = du.getLastMonth(du.getToday("yyyyMMdd"), "yyyyMMdd");
		}
		String curYear = bcycId.substring(0, 4);
//		//inputObject.getParams().put("bcycId",    curYear);
		inputObject.getParams().put("startMonth",curYear + "01");
		inputObject.getParams().put("endMonth",  curYear + "12");
		List<ScreenBean> list = this.getBaseDao().queryForList(
	            NAMESPACE + "cntAndmonthlydistrb", inputObject.getParams(),ScreenBean.class);
		
		JSONArray json = JSONArray.fromObject(list);

		// sortList(list);// 根据来桂人数排序
		// Collections.reverse(list);// 降序
		JSONArray json1 = JSONArray
				.fromObject(list.subList(0, list.size() > 3 ? 3 : list.size()));

		int totalNum = 0;
		for (ScreenBean obj : list) {
			totalNum += Integer.parseInt(obj.getVst_cnts());
		}
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("yearUserCount", totalNum);
        map.put("yearDataList", json);
        map.put("yearSortList", json1);
        JSONObject data = JSONObject.fromObject(map);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("regions",JsonUtil.Object2Json(data));
        outputObject.setObject(JsonUtil.Object2Json(resultMap));	
	}

    //全省游客分布
	@Override
	public void vstDistInProv(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		String bcycId=inputObject.getParams().get("bcycId");
		if (bcycId == null || "".equals(bcycId)) {
			DateUtil du = DateUtil.getInstance();
			bcycId = du.getLastMonth(du.getToday("yyyyMMdd"), "yyyyMMdd");
		}
		inputObject.getParams().put("bcycId",bcycId);
		List<ScreenBean> list = this.getBaseDao().queryForList(
	            NAMESPACE + "vstDistInProv", inputObject.getParams(),ScreenBean.class);
		double[] personCnt = new double[list.size()];
		int n = 0;
		for (ScreenBean bean : list) {
			personCnt[n++] = Double.parseDouble(bean.getVst_person_cnts());
		}

		List<Double> edgeInfo = BasicKMeans.getGroupEdgeValue(personCnt, 5);

		Map result = new HashMap();
		result.put("regionMapList", list);
		result.put("edgeInfo", edgeInfo);

		JSONObject data = JSONObject.fromObject(result);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		resultMap.put("regions", null == data ? "" : JsonUtil.Object2Json(data));
        outputObject.setObject(JsonUtil.Object2Json(resultMap));
	}

    //热度排行
	@Override
	public void hotRankSpotLastMonth(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		String bcycId=inputObject.getParams().get("bcycId");
		if (bcycId == null || "".equals(bcycId)) {
			DateUtil du = DateUtil.getInstance();
			bcycId = du.getLastMonth(du.getToday("yyyyMMdd"), "yyyyMMdd");
		}
		inputObject.getParams().put("bcycId",bcycId);
		List<ScreenBean> list = this.getBaseDao().queryForList(
	            NAMESPACE + "hotRankSpotLastMonth", inputObject.getParams(),ScreenBean.class);
		JSONArray data = JSONArray.fromObject(list);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		resultMap.put("regionMonthList", null == data ? "" : JsonUtil.Object2Json(data));
        outputObject.setObject(JsonUtil.Object2Json(resultMap));
	}

   //景区分类
	@Override
	public void regionClassList(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		String bcycId=inputObject.getParams().get("bcycId");
		String sqlMapId=inputObject.getParams().get("sqlMapId");
		if (bcycId == null || "".equals(bcycId)) {
			DateUtil du = DateUtil.getInstance();
			bcycId = du.getLastMonth(du.getToday("yyyyMMdd"), "yyyyMMdd");
		}
		inputObject.getParams().put("bcycId",bcycId);
		inputObject.getParams().put("sqlMapId",sqlMapId);
		List<ScreenBean> list = this.getBaseDao().queryForList(
	            NAMESPACE + "regionClassList", inputObject.getParams(),ScreenBean.class);
		JSONArray data = JSONArray.fromObject(list);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		resultMap.put("chartList", null == data ? "" : JsonUtil.Object2Json(data));
        outputObject.setObject(JsonUtil.Object2Json(resultMap));
		
	}

    //旅行社分类
	@Override
	public void agentClassList(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		String bcycId=inputObject.getParams().get("bcycId");
		String sqlMapId=inputObject.getParams().get("sqlMapId");
		if (bcycId == null || "".equals(bcycId)) {
			DateUtil du = DateUtil.getInstance();
			bcycId = du.getLastMonth(du.getToday("yyyyMMdd"), "yyyyMMdd");
		}
		inputObject.getParams().put("bcycId",bcycId);
		inputObject.getParams().put("sqlMapId",sqlMapId);
		List<ScreenBean> list = this.getBaseDao().queryForList(
				sqlMapId, inputObject.getParams(),ScreenBean.class);
		JSONArray data = JSONArray.fromObject(list);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		resultMap.put("chartList", null == data ? "" : JsonUtil.Object2Json(data));
        outputObject.setObject(JsonUtil.Object2Json(resultMap));
		
	}

	//酒店分类
	@Override
	public void hotelClassList(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		// TODO Auto-generated method stub
		
	}

	//导游分类
	@Override
	public void gudieClassList(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		// TODO Auto-generated method stub
		
	}

    //分类列表
	@Override
	public void queryClassList(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		
		String bcycId=inputObject.getParams().get("bcycId");
		String sqlMapId=inputObject.getParams().get("sqlMapId");
		if (bcycId == null || "".equals(bcycId)) {
			DateUtil du = DateUtil.getInstance();
			bcycId = du.getLastMonth(du.getToday("yyyyMMdd"), "yyyyMMdd");
		}
		inputObject.getParams().put("bcycId",bcycId);
		inputObject.getParams().put("sqlMapId",sqlMapId);
		List<ScreenBean> list = this.getBaseDao().queryForList(
				sqlMapId, inputObject.getParams(),ScreenBean.class);
		JSONArray data = JSONArray.fromObject(list);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		resultMap.put("chartList", null == data ? "" : JsonUtil.Object2Json(data));
        outputObject.setObject(JsonUtil.Object2Json(resultMap));

	}
}
